package gray.boolber.vk.web.api.utils;

import gray.boolber.vk.web.api.items.Attachment;
import gray.boolber.vk.web.api.items.Attachment.AApp;
import gray.boolber.vk.web.api.items.Attachment.AAudio;
import gray.boolber.vk.web.api.items.Attachment.ADoc;
import gray.boolber.vk.web.api.items.Attachment.AGraffiti;
import gray.boolber.vk.web.api.items.Attachment.ALink;
import gray.boolber.vk.web.api.items.Attachment.ANote;
import gray.boolber.vk.web.api.items.Attachment.APage;
import gray.boolber.vk.web.api.items.Attachment.APoll;
import gray.boolber.vk.web.api.items.Attachment.APostedPhoto;
import gray.boolber.vk.web.api.items.Attachment.AVideo;
import gray.boolber.vk.web.api.items.Comments;
import gray.boolber.vk.web.api.items.Friends;
import gray.boolber.vk.web.api.items.Group;
import gray.boolber.vk.web.api.items.Likes;
import gray.boolber.vk.web.api.items.News;
import gray.boolber.vk.web.api.items.Note;
import gray.boolber.vk.web.api.items.Photo;
import gray.boolber.vk.web.api.items.PhotoTag;
import gray.boolber.vk.web.api.items.Reposts;
import gray.boolber.vk.web.api.items.User;
import gray.boolber.vk.web.api.manager.NewsFeedManager;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class ApiParser {
	public static void parseNewsFeedResponse(String response){
		JSONObject jObject;
		NewsFeedManager nManager=NewsFeedManager.INSTANCE;
		try {
			jObject = new JSONObject(response).getJSONObject("response");
			nManager.newFrom=jObject.getString("new_from");
			nManager.newOffset=jObject.getString("new_offset");
			JSONArray jArray = jObject.getJSONArray("items");
			for(int i=0;i<jArray.length();i++){
				JSONObject jItem = jArray.getJSONObject(i);
				News item=new News();
				item.setSourceId(jItem.has("source_id")?jItem.getString("source_id"):"");
				item.setText(jItem.has("text")?jItem.getString("text"):"");
				item.setDate(jItem.has("date")?jItem.getString("date"):"");
				item.setAttachments(jItem.has("attachments")?parseAttachments(jItem.getString("attachments")):null);
				item.setComments(jItem.has("comments")?parseComments(jItem.getString("comments")):null);
				item.setLikes(jItem.has("likes")?parseLikes(jItem.getString("likes")):null);
				item.setReposts(jItem.has("reposts")?parseReposts(jItem.getString("reposts")):null);
				item.setCopyOwnerId(jItem.has("copy_owner_id")?jItem.getString("copy_owner_id"):"");
				item.setCopyPostDate(jItem.has("copy_post_date")?jItem.getString("copy_post_date"):"");
				item.setCopyPostId(jItem.has("copy_post_id")?jItem.getString("copy_post_id"):"");
				item.setPhotos(jItem.has("photos")?parsePhotos(jItem.getString("photos")):null);
				item.setFriends(jItem.has("friends")?parseFriends(jItem.getString("friends")):null);
				item.setNotes(jItem.has("notes")?parseNotes(jItem.getString("notes")):null);
				item.setPhotoTags(jItem.has("photo_tags")?parsePhotoTags(jItem.getString("photo_tags")):null);
				item.setAttachment(jItem.has("attachment")?Attachment.parseAttachment(jItem.getJSONObject("attachment")):null);
				nManager.addNewsItem(item);
			}
			jArray = jObject.getJSONArray("profiles");
			for(int i=0;i<jArray.length();i++){
				nManager.addProfileItem(User.parseUser(jArray.getJSONObject(i)));
			}
			jArray = jObject.getJSONArray("groups");
			for(int i=0;i<jArray.length();i++){
				nManager.addGroupItem(Group.parseGroup(jArray.getJSONObject(i)));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}finally{
			System.gc();
		}
		int y=0; 
		y++;
	}
	
	public static ArrayList<Attachment> parseAttachments(String response){
		ArrayList<Attachment> attachments=new ArrayList<Attachment>();
		try {
			JSONArray jArr = new JSONArray(response);
			for(int i=0;i<jArr.length();i++){
				if(jArr.optJSONObject(i)!=null){
					attachments.add(Attachment.parseAttachment(jArr.getJSONObject(i)));
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		} 
		return attachments;
	}
	
	public static Likes parseLikes(String response){
		Likes likes=new Likes();
		try {
			JSONObject jObject = new JSONObject(response);
			likes.setCount(jObject.getString("count"));
			likes.setUserLikes(jObject.getString("user_likes"));
			likes.setCanLike(jObject.getString("can_like"));
			likes.setCanPublish(jObject.getString("can_publish"));
		} catch (JSONException e) {
			e.printStackTrace();
		} 
		return likes;
	}
	public static Reposts parseReposts(String response){
		Reposts reposts=new Reposts();
		try {
			JSONObject jObject = new JSONObject(response);
			reposts.setCount(jObject.getString("count"));
			reposts.setUser_reposted(jObject.getString("user_reposted"));
		} catch (JSONException e) {
			e.printStackTrace();
		} 
		return reposts;
	}
	public static Comments parseComments(String response){
		Comments comments=new Comments();
		try {
			JSONObject jObject = new JSONObject(response);
			comments.setCount(jObject.getString("count"));
			comments.setCanPost(jObject.getString("can_post"));
		} catch (JSONException e) {
			e.printStackTrace();
		} 
		return comments;
	}
	public static ArrayList<Friends> parseFriends(String response){
		ArrayList<Friends> friends=new ArrayList<Friends>();
		try {
			JSONArray jArr = new JSONArray(response);
			for(int i=0;i<jArr.length();i++){
				if(jArr.optJSONObject(i)!=null){
					Friends fr=new Friends();
					fr.setUid(jArr.getJSONObject(i).getString("uid"));
					friends.add(fr);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		} 
		return friends;
	} 
	public static ArrayList<Photo> parsePhotos(String response){
		ArrayList<Photo> photos=new ArrayList<Photo>();
		try {
			JSONArray jArr = new JSONArray(response);
			for(int i=0;i<jArr.length();i++){
				if(jArr.optJSONObject(i)!=null){
					photos.add(Photo.parsePhoto(jArr.getJSONObject(i)));
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		} 
		return photos;
	}
	public static ArrayList<PhotoTag> parsePhotoTags(String response){
		ArrayList<PhotoTag> photos=new ArrayList<PhotoTag>();
		try {
			JSONArray jArr = new JSONArray(response);
			for(int i=0;i<jArr.length();i++){
				
				PhotoTag fr=new PhotoTag();
				if(jArr.optJSONObject(i)!=null){
					fr.pid=jArr.getJSONObject(i).getString("pid");
					fr.ownerId=jArr.getJSONObject(i).getString("owner_id");
					fr.src=jArr.getJSONObject(i).getString("src");
					fr.srcBig=jArr.getJSONObject(i).getString("src_big");
					fr.aid=jArr.getJSONObject(i).getString("aid");
					fr.created=jArr.getJSONObject(i).getString("created");
					fr.srcSmall=jArr.getJSONObject(i).getString("src_small");
					fr.srcXBig=jArr.getJSONObject(i).getString("src_xbig");
					fr.text=jArr.getJSONObject(i).getString("text");
					photos.add(fr);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		} 
		return photos;
	} 
	public static ArrayList<Note> parseNotes(String response){
		ArrayList<Note> notes=new ArrayList<Note>();
		try {
			JSONArray jArr = new JSONArray(response);
			for(int i=0;i<jArr.length();i++){
				Note fr=new Note();
				if(jArr.optJSONObject(i)!=null){
					fr.title=jArr.getJSONObject(i).getString("title");
					fr.ownerId=jArr.getJSONObject(i).getString("owner_id");
					fr.ncom=jArr.getJSONObject(i).getString("ncom");
					fr.nid=jArr.getJSONObject(i).getString("owner_id");
					notes.add(fr);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		} 
		return notes;
	} 
	

}
